﻿//tyh添加
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Estimate.Common.Entity;
using Estimate.Common.DBFieldTypes;

namespace Estimate.Common.Estimate
{
    /// <summary>
    /// 全局可以共用的方法
    /// </summary>
  public static  class GlobalFunction
    {
      //span_kind类型
      private static Dictionary<ScheduleSpanKind, List<ForeSpanType>> SpanKinds = new Dictionary<ScheduleSpanKind, List<ForeSpanType>>();

      static GlobalFunction()
      {
          SpanKinds.Add(ScheduleSpanKind.st, new List<ForeSpanType>() { ForeSpanType.FD00_12, 
                                                                        ForeSpanType.FD12_24, 
                                                                        ForeSpanType.FD00_24, 
                                                                        ForeSpanType.FD24_36, 
		                                                                ForeSpanType.FD36_48, 
                                                                        ForeSpanType.FD24_48, 
		                                                                ForeSpanType.FD48_60, 
		                                                                ForeSpanType.FD60_72, 
                                                                        ForeSpanType.FD48_72});

          SpanKinds.Add(ScheduleSpanKind.md, new List<ForeSpanType>() { ForeSpanType.FD72_84,
                                                                        ForeSpanType.FD84_96,
                                                                        ForeSpanType.FD72_96,
                                                                        ForeSpanType.FD96_108, 
                                                                        ForeSpanType.FD108_120, 
		                                                                ForeSpanType.FD96_120, 
                                                                        ForeSpanType.FD120_132, 
		                                                                ForeSpanType.FD132_144, 
		                                                                ForeSpanType.FD120_144, 
                                                                        ForeSpanType.FD144_156,
		                                                                ForeSpanType.FD156_168,
		                                                                ForeSpanType.FD144_168});

          SpanKinds.Add(ScheduleSpanKind.sm, new List<ForeSpanType>() { ForeSpanType.FD00_12, 
                                                                        ForeSpanType.FD12_24, 
                                                                        ForeSpanType.FD00_24, 
                                                                        ForeSpanType.FD24_36, 
		                                                                ForeSpanType.FD36_48, 
                                                                        ForeSpanType.FD24_48, 
		                                                                ForeSpanType.FD48_60, 
		                                                                ForeSpanType.FD60_72, 
                                                                        ForeSpanType.FD48_72,
		                                                                ForeSpanType.FD72_84,
		                                                                ForeSpanType.FD84_96,
                                                                        ForeSpanType.FD72_96,
                                                                        ForeSpanType.FD84_96, 
                                                                        ForeSpanType.FD72_96, 
                                                                        ForeSpanType.FD96_108, 
                                                                        ForeSpanType.FD108_120, 
		                                                                ForeSpanType.FD96_120, 
                                                                        ForeSpanType.FD120_132, 
		                                                                ForeSpanType.FD132_144, 
		                                                                ForeSpanType.FD120_144, 
                                                                        ForeSpanType.FD144_156,
		                                                                ForeSpanType.FD156_168,
		                                                                ForeSpanType.FD144_168});

                                                                          
      }
      /// <summary>
      /// 查找当前小semi的站点(station)对应的结果字典(key)
      /// </summary>
      /// <param name="station"></param>
      /// <param name="CityCodes"></param>
      /// <returns></returns>
      public static bool CitiesContains(List<string> keys, string station, Dictionary<string, string> CityCodes)
      {
          foreach (KeyValuePair<string, string> pair in CityCodes)
          {
              if (pair.Value == "00001")
              {
                  //用于省台成绩计算，应为省台预报数据是单独的表，检索出来的所有记录都要统计
                  keys.Add( pair.Value);
                  return true;
              }
              if (pair.Value.Contains(station))
              {
                  keys.Add(pair.Value);
                  return true;
              }
          }
          return false;
      }

      /// <summary>
      /// 查找当前小semi对应的预报员Key
      /// </summary>
      /// <param name="keys">存放预报员Key的字典,字典key为"预报员Key"，value为"需要记录样本的站点"</param>
      /// <param name="station">当前semi的站点</param>
      /// <param name="semi"></param>
      /// <param name="IndividualSchedules"></param>
      /// <returns></returns>
      public static bool SchedulesContains(List<string> keys, string station, ForeEntity semi, Dictionary<DateTime, Dictionary<ForeClockType, Dictionary<ForeSendpointType, List<ScheduleEntity>>>> Schedules)
      {
          bool result = false;
          if (!Schedules.ContainsKey(semi.Date))
              return false;
          if (!Schedules[semi.Date].ContainsKey(semi.Clock))
              return false;
          if (!Schedules[semi.Date][semi.Clock].ContainsKey(semi.Sendpoint))
              return false;

          foreach (ScheduleEntity schedule in Schedules[semi.Date][semi.Clock][semi.Sendpoint])
          {
              if ( SpanKinds[schedule.SpanKind].Contains(semi.Span) && schedule.Stations.Contains(station))
              {
                  string key = AssemblyForecastNum(schedule);
                  if (!keys.Contains(key))
                      keys.Add(key);

                  result = true;
              }
          }
          return result;
      }

      /// <summary>
      /// 组合预报员KEY
      /// 格式为"站点号_预报员编号;站点名称"
      /// </summary>
      /// <param name="schedule"></param>
      /// <returns></returns>
      public static string AssemblyForecastNum(ScheduleEntity schedule)
      {
          return schedule.Station + "_" + schedule.ForecastNumber + ";" + schedule.StationName;
      }

      /// <summary>
      /// 分割格式为"站点号_预报员编号;站点名称"的预报员KEY
      /// 返回预报员编号数组
      /// </summary>
      /// <param name="ForecastNum"></param>
      /// <param name="stationName">站点名称</param>
      /// <returns>返回预报员编号</returns>
      public static string SplitForecastNum(string ForecastNum, ref string stationName)
      {
          string[] strs = ForecastNum.Split(';');
          stationName = strs[1];
          return strs[0];
      }

      /// <summary>
      /// 记录样本数
      /// </summary>
      /// <param name="RecordCount"></param>
      /// <param name="key">为站台编号或为预报员编号</param>
      /// <param name="yyyyMM">smei的日期:年月</param>
      /// <param name="span"></param>
      /// <param name="count"></param>
      public static void SetRecordCount(Dictionary<string, Dictionary<ForeSpanType, Dictionary<DateTime, int>>> RecordCount, string key, ForeSpanType span, DateTime date, int count)
      {
          if (!RecordCount[key][span].Keys.Contains(date))
              RecordCount[key][span].Add(date, count);
          else
              RecordCount[key][span][date] += count;
      }


      //public static void SetCollectRecordCount(Dictionary<string, Dictionary<ForeSpanType, int>> IndividualRecordCount, string key, DateTime date, ForeClockType clock, int count)
      //{
      //    //string countKey = date.ToString("yyyyMMdd") + clock.ToString();
      //    //if (!IndividualRecordCount[key].ContainsKey(countKey))
      //    //    IndividualRecordCount[key][countKey] = 0;
      //    //IndividualRecordCount[key][countKey] += count;
      //}

      /// <summary>
      /// 计算降水、晴雨技巧值
      /// </summary>
      public static float CalculateWaterSkill(float TS,  float StandardTS)
      {

          float Water_Skill = 0;
          if (StandardTS != 101.0f && TS != 101.0f)
          {
              if (StandardTS != 100)
                  Water_Skill = (TS - StandardTS) / (100f - StandardTS);
              else
                  Water_Skill = 0;
          }
          return Water_Skill;
      }

      /// <summary>
      /// 计算温度技巧值
      /// </summary>
      /// <param name="memberTempScore"></param>
      /// <param name="standardTempScore"></param>
      /// <returns></returns>
      public static float CalculateTempSkill(float TS, float StandardTS)
      {

          float Temp_Skill = 0;

          if (TS != 0f && StandardTS != 0f)
              Temp_Skill = (TS - StandardTS) / StandardTS;

          return Temp_Skill;

      }

    }
}
